package com.hdu.clj.dncard.util

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.hdu.clj.dncard.*

class MapAdapter(val zxMapList:List<ZxMap>,context: Context) : RecyclerView.Adapter<MapAdapter.ViewHolder>(){

    val context = context

    val maplist = arrayOf(Map1Activity::class.java,Map2Activity::class.java,Map3Activity::class.java,Map4Activity::class.java)

    inner class ViewHolder(view: View):RecyclerView.ViewHolder(view){
        val mapImage : ImageView = view.findViewById(R.id.mapImage)
        val mapName : TextView = view.findViewById(R.id.mapName)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.map_item,parent,false)
        val viewHolder = ViewHolder(view)
        viewHolder.itemView.setOnClickListener {

            val position = viewHolder.adapterPosition
            if (position==0||LoadAndSave().load("$position",context).isNotEmpty()){
                val intent = Intent(context,maplist[position])
                context.startActivity(intent)
            }
            else{
                Toast.makeText(context,"未完成前置关卡！",Toast.LENGTH_SHORT).show()
            }

        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val zxMap = zxMapList[position]
        holder.mapImage.setImageResource(zxMap.ZxMapImage)
        holder.mapName.text = zxMap.ZxMapName
    }

    override fun getItemCount() = zxMapList.size

}